
package com.distribute.framework.dao.impl;

import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.core.web.util.Page;
import com.distribute.framework.core.util.Function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.distribute.framework.core.sql.mongodb.MongoDaoSupport;
import com.distribute.framework.core.sql.mongodb.MongodbDataSource;
import com.distribute.framework.core.sql.Table;

import org.springframework.stereotype.Repository;
import javax.annotation.Resource;

import com.distribute.framework.plugin.*;
import com.distribute.framework.dao.IMongoDao;

/**
 * 通用Mongo的Dao实现
 * @author 飞啊飘啊
 * 感谢mongodb的nosql框架
 */
@SuppressWarnings("unchecked")
@Repository(value="distributeDataDao")
public class DistributeDataDao extends MongoDaoSupport<Map> implements IMongoDao
{

	@Resource(name="distributeDataDataSource")
	MongodbDataSource dataSource;
	@Override
	public MongodbDataSource getDataSource() {
		return dataSource;
	}
	/************LAZY3Q_DEFINE_CODE************/
	/************LAZY3Q_DEFINE_CODE************/

	/**
	 * 自定义加工处理params,可以添加表别名、联表查询等操作
	 * 可在此动态设置数据源，以达到分库分表的目的
	 * @param params
	 * @return
	 */
	private Map custom(Table table,Map params){
		/************LAZY3Q_DAO_CUSTOM************/
		/************LAZY3Q_DAO_CUSTOM************/
		return params;
	}
	/************LAZY3Q_DEFINE_FUNCTION************/
	/************LAZY3Q_DEFINE_FUNCTION************/
	

	/**
	 * 获取所有对象
	 * @return
	 */	
	public List<Map> getMapList(Table table,Map params,Integer offset,Integer size)
	{		
		params = custom(table,params);
		List<Map> list = this.queryForList(table,params, offset, size);
		return list;
	}
	
	/**
	 * 获取对象分页
	 * @return
	 */
	public Page getMapPage(Table table,int page,int size,Map params)
	{
		params = custom(table,params);
		
		if(size<1)size=20;
		
		//获取分页总数
		Long count=getMapCount(table,params);
		//取分页的队列
		List list=this.queryForList(table,params, (int)Page.test(page,size,count), size);
		//返回一个包装分页对象
		return new Page(page,size,count,list);
	}
	
	/**
	 * 获取对象数量
	 * @return
	 */	
	public Long getMapCount(Table table,Map params)
	{
		params = custom(table,params);
		return this.queryCount(table,params);
	}
	
	/**
	 * 根据查询条件取对象
	 * @param params
	 * @return
	 */
	public Map getMap(Table table,Map params)
	{		
		params = custom(table,params);
			
		List<Map> list=this.getMapList(table,params,0,1);
		Map object = list.isEmpty()?null:list.get(0);
		
		return object;
	}
		
	/**
	 * 根据ID取对象
	 * @param id
	 * @return
	 */
	public Map getMapById(Table table,Object id)
	{
		if(id==null)return null;
		Map params=new HashMap();
		params.put(table.getPrimaryName(), id);
		params = custom(table,params);
		return (Map) this.queryForObject(table,params);
	}
	

	/**
	 * 保存对象
	 * @param object
	 */
	public Map saveMap(Table table,Map object)
	{
		object = custom(table,object);
		object = this.insert(table,object);
		return object;
	}

	

	/**
	 * 更新对象
	 * @param object
	 */
	public int updateMap(Table table,Map object)
	{
		object = custom(table,object);
		int rows = this.update(table,object);
		return rows;
	}
	/**
	 * 更新对象非空字段
	 * @param object
	 */
	public int updateMap$NotNull(Table table,Map object)
	{
		object = custom(table,object);
		int rows = this.update(table,object);
		return rows;
	}

	/**
	 * 根据ID删除一个对象
	 * @param id
	 */
	public void deleteMapById(Table table,Object id)
	{
		Map params=new HashMap();
		params.put(table.getPrimaryName(), id);
		params = custom(table,params);
		this.delete(table,params);
		
	}
	/**
	 * 根据条件更新对象
	 * @param values
	 * @param params
	 */
	public int updateMapByConfirm(Table table,Map values, Map params){
		params = custom(table,params);
		int rows = this.update(table,params, values);
		return rows;
	}
	/**
	 * 根据条件删除对象
	 * @param params
	 */
	public void deleteMapByConfirm(Table table,Map params){
		params = custom(table,params);
		this.delete(table,params);
	}

}